import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class LoadingWidget {
  static bool _loading = false;
  static OverlayEntry? _overlayEntry;

  static void showLoading(BuildContext context, {String? text}) {
    if (_loading == true) {
      return;
    }
    _loading = true;
    _overlayEntry = OverlayEntry(builder: (context) {
      return WillPopScope(
        onWillPop: () async {
          dismissLoading();
          return false;
        },
        child: GestureDetector(
          onTap: (){

          },
          child: Container(
            width: double.infinity,
            height: double.infinity,
            child: Center(
              child: Container(
                constraints: BoxConstraints(
                  minHeight: 80,
                  maxHeight: 80,
                  minWidth: 80,
                  maxWidth: 100,
                ),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10),
                  color: Colors.black87,
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    CircularProgressIndicator(),
                    const SizedBox(
                      height: 10,
                    ),
                    Text(
                      text ?? "正在加载",
                      style: Theme.of(context)
                          .primaryTextTheme
                          .bodyText1!
                          .copyWith(color: Colors.white, fontSize: 12),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      );
    });
    Overlay.of(context)!.insert(_overlayEntry!);
  }

  static void dismissLoading() {
    _overlayEntry?.remove();
    _overlayEntry = null;
    _loading = false;
  }
}
